package com.gmss.crawler.module.test.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gmss.crawler.constants.Base;
import com.gmss.crawler.enums.AcademicianEnum;
import com.gmss.crawler.enums.CommonEnum;
import com.gmss.crawler.module.company.domain.Company;
import com.gmss.crawler.module.company.service.ICompanyService;
import com.gmss.crawler.module.honor.domain.Honor;
import com.gmss.crawler.module.honor.service.IHonorService;
import com.gmss.crawler.module.integrity.integritycase.service.IIntegrityCaseService;
import com.gmss.crawler.module.person.domain.PersonInfo;
import com.gmss.crawler.module.social.domain.Social;
import com.gmss.crawler.module.social.service.ISocialService;
import com.gmss.crawler.module.test.domain.PersonInfoTopy;
import com.gmss.crawler.module.test.serviece.IYouthService;
import com.gmss.crawler.module.testtitle.domain.PersonTitleToyp;
import com.gmss.crawler.module.testtitle.service.PersonTitleToypService;
import com.gmss.crawler.service.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/youth")
public class YouthController extends Base {

    @Resource
    private YouthService youthService;
    @Resource
    private IYouthService iYouthService;
    @Resource
    private PersonTitleToypService personTitleToypService;
    @Resource
    private ICompanyService iCompanyService;
    @Resource
    private ScholarService scholarService;
    @Resource
    private CasCrawlerService casCrawlerService;
    @Resource
    private ParseExcelService parseExcelService;
    @Resource
    private IntegrityCaseCrawlerService integrityCaseCrawlerService;
    @Resource
    private IIntegrityCaseService iIntegrityCaseService;
    @Resource
    private ISocialService iSocialService;
    @Resource
    private IHonorService iHonorService;

    @PostMapping("/saveYouthInfoByJQ")
    public String save1() {
        try {
            List<PersonInfoTopy> personInfolist = youthService.crawlerData();
            Map<String, PersonInfoTopy> map = new HashMap<>();
            for (int i = 0; i < personInfolist.size(); i++) {
                PersonTitleToyp personTitleToyp = new PersonTitleToyp();
                BeanUtil.copyProperties(personInfolist.get(i), personTitleToyp);
                LambdaQueryWrapper<PersonInfoTopy> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(PersonInfoTopy::getName, personInfolist.get(i).getName());
                queryWrapper.eq(PersonInfoTopy::getMajor, personInfolist.get(i).getMajor());
                queryWrapper.eq(PersonInfoTopy::getStatus, 1);
                queryWrapper.eq(PersonInfoTopy::getDelFlag, 1);
                //可能有重名的  所以用list查询
                List<PersonInfoTopy> list = iYouthService.list(queryWrapper);

                if (CollectionUtils.isNotEmpty(list)) {
                    for (PersonInfoTopy personInfoTopy : list) {
                        Long id = personInfoTopy.getId();
                        personTitleToyp.setPersonId(id);
                        personTitleToypService.save(personTitleToyp);
                    }
                } else {
                    iYouthService.queryCompany(personInfolist.get(i));
                    iYouthService.save(personInfolist.get(i));
                    //查询插入数据的personId
                    LambdaQueryWrapper<PersonInfoTopy> queryWrapper1 = new LambdaQueryWrapper<>();
                    queryWrapper1.eq(PersonInfoTopy::getName, personInfolist.get(i).getName());
                    queryWrapper1.eq(PersonInfoTopy::getMajor, personInfolist.get(i).getMajor());
                    PersonInfoTopy infoTopy1 = iYouthService.getOne(queryWrapper1);
                    //获取id
                    Long id = infoTopy1.getId();
                    //拿到personId 存入 title中
                    personTitleToyp.setPersonId(id);
                    personTitleToypService.save(personTitleToyp);
                }
            }
//            map.forEach((key,value) -> {
//                PersonTitleToyp personTitleToyp = new PersonTitleToyp();
//                //1.把数据插入info表中
//                //查询工作单位id
//                iYouthService.queryCompany(value);
//                iYouthService.save(value);
//                //查询插入数据的personId
//                LambdaQueryWrapper<PersonInfoTopy> queryWrapper1 = new LambdaQueryWrapper<>();
//                queryWrapper1.eq(PersonInfoTopy::getName, value.getName());
//                queryWrapper1.eq(PersonInfoTopy::getMajor, value.getMajor());
//                PersonInfoTopy infoTopy1 = iYouthService.getOne(queryWrapper1);
//                //获取id
//                Long id = infoTopy1.getId();
//                //拿到personId 存入 title中
//                personTitleToyp.setPersonId(id);
//                personTitleToyp.setElectionYear(value.getElectionYear());
//                personTitleToyp.setAcademicianTitle(value.getAcademicianTitle());
//                personTitleToypService.save(personTitleToyp);
//            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        return respJson(CommonEnum.SUCCESS);
    }

    @PostMapping("/save")
    public String save() {
        try {
//            List<PersonInfoTopy> list1 = new ArrayList();
//            PersonInfoTopy personInfoTopy = new PersonInfoTopy();
//            personInfoTopy.setWorkCompany("东南大学");
//            personInfoTopy.setName("吴刚");
//            personInfoTopy.setMajor("结构工程");
//            list1.add(personInfoTopy);
            List<PersonInfoTopy> infoTopies = youthService.crawlerData();
            Map<String, PersonInfoTopy> map = new LinkedHashMap<>();
            for (int i = 0; i < infoTopies.size(); i++) {
                PersonTitleToyp personTitleToyp = new PersonTitleToyp();
                BeanUtil.copyProperties(infoTopies.get(i), personTitleToyp);
                LambdaQueryWrapper<PersonInfoTopy> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(PersonInfoTopy::getName, infoTopies.get(i).getName());
                queryWrapper.eq(PersonInfoTopy::getStatus, 1);
                queryWrapper.eq(PersonInfoTopy::getDelFlag, 1);
                //可能有重名的  所以用list查询
                List<PersonInfoTopy> list = iYouthService.list(queryWrapper);
                if (CollectionUtils.isNotEmpty(list)) {
                    for (PersonInfoTopy infoTopy : list) {
                        //如果infoTopy不等于null  说明在数据库有相同的名字  比较他们的工作单位是否相同
                        Long companyId = infoTopy.getCompanyId();
                        LambdaQueryWrapper<Company> companyQueryWrapper = new LambdaQueryWrapper<>();
                        //根据companyId查询company表
                        companyQueryWrapper.eq(Company::getId, companyId);
                        Company company = iCompanyService.getOne(companyQueryWrapper);
                        //比较他们的工作单位是否相同
                        if (company != null) {
                            if (infoTopies.get(i).getWorkCompany().equals(company.getCompanyName())) {
                                //如果他们的工作单位也相同      比较他们的专业是否一致
                                if (infoTopies.get(i).getMajor().equals(infoTopy.getMajor())) {
                                    //如果相同  说明数据库和插入的人是同一个人   头衔不同
                                    //传入personId
                                    personTitleToyp.setPersonId(infoTopy.getId());
                                    //存入数据库
                                    personTitleToypService.save(personTitleToyp);
                                } else {
                                    //如果不同  可能是专业名称没有匹配上  也可能是不是同一个人  查询数据库中有没有有关这个人的头衔
                                    //1.查询这个姓名有没有其他的院士或者长江学者头衔
                                    LambdaQueryWrapper<PersonTitleToyp> titleQueryWrapper = new LambdaQueryWrapper<>();
                                    titleQueryWrapper.eq(PersonTitleToyp::getPersonId, infoTopy.getId());
                                    List<PersonTitleToyp> list2 = personTitleToypService.list(titleQueryWrapper);
                                    for (PersonTitleToyp titleToyp : list2) {
                                        if (StringUtils.containsAny(titleToyp.getAcademicianTitleValue(), "院士", "长江学者")) {
                                            //如果有院士或者长江学者的头衔   在title表中插入一条数据
                                            //1.设置personId
                                            personTitleToyp.setPersonId(titleToyp.getPersonId());
                                            //插入数据
                                            personTitleToypService.save(personTitleToyp);
                                        } else {
                                            //如果查不到头衔   说明不是同一个人    在info表和title表中分别插入数据
                                            //1.把数据插入info表中
                                            //查询工作单位id
                                            iYouthService.queryCompany(infoTopies.get(i));
                                            iYouthService.save(infoTopies.get(i));
                                            //查询插入数据的personId
                                            LambdaQueryWrapper<PersonInfoTopy> queryWrapper1 = new LambdaQueryWrapper<>();
                                            queryWrapper1.eq(PersonInfoTopy::getName, infoTopies.get(i).getName());
                                            queryWrapper1.eq(PersonInfoTopy::getMajor, infoTopies.get(i).getMajor());
                                            List<PersonInfoTopy> list1 = iYouthService.list(queryWrapper1);
                                            //获取id
                                            Long id = list1.get(0).getId();
                                            //拿到personId 存入 title中
                                            personTitleToyp.setPersonId(id);
                                            personTitleToypService.save(personTitleToyp);
                                        }
                                    }

                                }
                            } else if (!infoTopies.get(i).getWorkCompany().equals(company.getCompanyName())) {
                                if (infoTopies.get(i).getMajor().equals(infoTopy.getMajor())) {
                                    //姓名和专业相同   认为是同一个人
                                    Long id = infoTopy.getId();
                                    personTitleToyp.setPersonId(id);
                                    personTitleToypService.save(personTitleToyp);
                                } else {
                                    map.put(infoTopies.get(i).getName(), infoTopies.get(i));
                                }
                            }
                        }
                    }
                } else {
                    //没有查到同名的人    说明都是不同的人
                    //1.把数据插入info表中
                    //查询工作单位id
                    iYouthService.queryCompany(infoTopies.get(i));
                    iYouthService.save(infoTopies.get(i));
                    //2.查询插入数据的personId
                    LambdaQueryWrapper<PersonInfoTopy> queryWrapper1 = new LambdaQueryWrapper<>();
                    queryWrapper1.eq(PersonInfoTopy::getName, infoTopies.get(i).getName());
                    queryWrapper1.eq(PersonInfoTopy::getMajor, infoTopies.get(i).getMajor());
                    PersonInfoTopy one = iYouthService.getOne(queryWrapper1);
                    //获取id
                    Long id = one.getId();
                    //拿到personId 存入 title中
                    personTitleToyp.setPersonId(id);
                    personTitleToypService.save(personTitleToyp);
                }
            }
            map.forEach((key, value) -> {
                PersonTitleToyp personTitleToyp = new PersonTitleToyp();
                //1.把数据插入info表中
                //查询工作单位id
                iYouthService.queryCompany(value);
                iYouthService.save(value);
                //查询插入数据的personId
                LambdaQueryWrapper<PersonInfoTopy> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(PersonInfoTopy::getName, value.getName());
                queryWrapper1.eq(PersonInfoTopy::getMajor, value.getMajor());
                List<PersonInfoTopy> list = iYouthService.list(queryWrapper1);
                //获取id
                Long id = list.get(list.size() - 1).getId();
                //拿到personId 存入 title中
                personTitleToyp.setPersonId(id);
                personTitleToyp.setElectionYear(value.getElectionYear());
                personTitleToyp.setAcademicianTitle(value.getAcademicianTitle());
                personTitleToypService.save(personTitleToyp);
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        return respJson(CommonEnum.SUCCESS);
    }

    @PostMapping("/addData")
    public String addData() throws IOException {
        List<PersonInfo> personInfoList = scholarService.parseData();
        List<PersonInfoTopy> personInfoTopies = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(personInfoList)) {
            for (PersonInfo personInfo : personInfoList) {
                PersonInfoTopy personInfoTopy = new PersonInfoTopy();
                BeanUtil.copyProperties(personInfo, personInfoTopy);
                personInfoTopies.add(personInfoTopy);
            }
        }
        Map<String, PersonInfoTopy> map = new HashMap<>();
        for (int i = 0; i < personInfoTopies.size(); i++) {
            PersonTitleToyp personTitleToyp = new PersonTitleToyp();
            BeanUtil.copyProperties(personInfoTopies.get(i), personTitleToyp);
            //查找数据库看是否有相同名字的人
            LambdaQueryWrapper<PersonInfoTopy> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PersonInfoTopy::getName, personInfoTopies.get(i).getName());
            queryWrapper.eq(PersonInfoTopy::getStatus, 1);
            queryWrapper.eq(PersonInfoTopy::getDelFlag, 1);
            List<PersonInfoTopy> list = iYouthService.list(queryWrapper);
            if (CollectionUtils.isNotEmpty(list)) {
                //说明名字一样，查询工作地点是否相同
                for (PersonInfoTopy personInfoTopy : list) {
                    LambdaQueryWrapper<Company> companyWrapper = new LambdaQueryWrapper<>();
                    companyWrapper.eq(Company::getId, personInfoTopy.getCompanyId());
                    Company company = iCompanyService.getOne(companyWrapper);
                    //如果company不为空
                    if (company != null) {
                        if (StringUtils.equals(company.getCompanyName(), personInfoTopies.get(i).getWorkCompany())) {
                            //如果所在单位相同
                            //比较他们的专业是否相同
                            if (StringUtils.equals(personInfoTopies.get(i).getMajor(), personInfoTopy.getMajor())) {
                                //则认为是同一个人
                                personTitleToyp.setPersonId(personInfoTopy.getId());
                                personTitleToypService.save(personTitleToyp);
                            } else {
                                //如果不同  可能是专业名称没有匹配上  也可能是同一个人  查询数据库中有没有有关这个人的头衔
                                //1.查询这个姓名有没有其他的院士
                                LambdaQueryWrapper<PersonTitleToyp> titleQueryWrapper = new LambdaQueryWrapper<>();
                                titleQueryWrapper.eq(PersonTitleToyp::getPersonId, personInfoTopy.getId());
                                List<PersonTitleToyp> list2 = personTitleToypService.list(titleQueryWrapper);
                                for (PersonTitleToyp titleToyp : list2) {
                                    if (StringUtils.contains(titleToyp.getAcademicianTitleValue(), "院士")) {
                                        //如果有院士或者长江学者的头衔   在title表中插入一条数据
                                        //1.设置personId
                                        personTitleToyp.setPersonId(titleToyp.getPersonId());
                                        //插入数据
                                        personTitleToypService.save(personTitleToyp);
                                    } else {
                                        //如果查不到头衔   说明不是同一个人    在info表和title表中分别插入数据
                                        //1.把数据插入info表中
                                        //查询工作单位id
                                        iYouthService.queryCompany(personInfoTopies.get(i));
                                        iYouthService.save(personInfoTopies.get(i));
                                        //查询插入数据的personId
                                        LambdaQueryWrapper<PersonInfoTopy> queryWrapper1 = new LambdaQueryWrapper<>();
                                        queryWrapper1.eq(PersonInfoTopy::getName, personInfoTopies.get(i).getName());
                                        queryWrapper1.eq(PersonInfoTopy::getMajor, personInfoTopies.get(i).getMajor());
                                        List<PersonInfoTopy> list1 = iYouthService.list(queryWrapper1);
                                        //获取id
                                        Long id = list1.get(0).getId();
                                        //拿到personId 存入 title中
                                        personTitleToyp.setPersonId(id);
                                        personTitleToypService.save(personTitleToyp);
                                    }
                                }
                            }
                        } else if (!StringUtils.equals(personInfoTopies.get(i).getWorkCompany(), personInfoTopy.getWorkCompany())) {
                            //如果名字和专业相同    所在单位不同   也认为是一个人
                            if (StringUtils.equals(personInfoTopies.get(i).getMajor(), personInfoTopy.getMajor())) {
                                personTitleToyp.setPersonId(personInfoTopy.getId());
                                personTitleToypService.save(personTitleToyp);
                            } else {
                                map.put(personInfoTopies.get(i).getName(), personInfoTopies.get(i));
                            }
                        }
                    }
                }
            } else {
                //没有查到同名的人    说明都是不同的人
                //1.把数据插入info表中
                //查询工作单位id
                iYouthService.queryCompany(personInfoTopies.get(i));
                iYouthService.save(personInfoTopies.get(i));
                //2.查询插入数据的personId
                LambdaQueryWrapper<PersonInfoTopy> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(PersonInfoTopy::getName, personInfoTopies.get(i).getName());
                queryWrapper1.eq(PersonInfoTopy::getMajor, personInfoTopies.get(i).getMajor());
                PersonInfoTopy one = iYouthService.getOne(queryWrapper1);
                //获取id
                Long id = one.getId();
                //拿到personId 存入 title中
                personTitleToyp.setPersonId(id);
                personTitleToypService.save(personTitleToyp);
            }
        }
        map.forEach((key, value) -> {
            PersonTitleToyp personTitleToyp = new PersonTitleToyp();
            //1.把数据插入info表中
            //查询工作单位id
            iYouthService.queryCompany(value);
            iYouthService.save(value);
            //查询插入数据的personId
            LambdaQueryWrapper<PersonInfoTopy> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(PersonInfoTopy::getName, value.getName());
            queryWrapper1.eq(PersonInfoTopy::getMajor, value.getMajor());
            List<PersonInfoTopy> list = iYouthService.list(queryWrapper1);
            //获取id
            Long id = list.get(list.size() - 1).getId();
            //拿到personId 存入 title中
            personTitleToyp.setPersonId(id);
            personTitleToyp.setElectionYear(value.getElectionYear());
            personTitleToyp.setAcademicianTitle(value.getAcademicianTitle());
            personTitleToypService.save(personTitleToyp);
        });
        return respJson(CommonEnum.SUCCESS);
    }


    public static List<String> getAcademician() {
        List<String> list = new ArrayList<>();
        list.add(AcademicianEnum.CAS_ACADEMICIAN.getCode().toString());
        list.add(AcademicianEnum.CAS_DIE_ACADEMICIAN.getCode().toString());
        list.add(AcademicianEnum.CAS_FOREIGN_ACADEMICIAN.getCode().toString());
        list.add(AcademicianEnum.CAS_DIE_FOREIGN_ACADEMICIAN.getCode().toString());
        list.add(AcademicianEnum.CAE_ACADEMICIAN.getCode().toString());
        list.add(AcademicianEnum.CAE_DIE_ACADEMICIAN.getCode().toString());
        list.add(AcademicianEnum.CAE_FOREIGN_ACADEMICIAN.getCode().toString());
        list.add(AcademicianEnum.CAE_DIE_FOREIGN_ACADEMICIAN.getCode().toString());
        list.add(AcademicianEnum.TWO_CAS_CAE.getCode().toString());
        list.add(AcademicianEnum.CHANGJIANG_SCHOLAR_SPECIAL.toString());
        list.add(AcademicianEnum.CHANGJIANG_SCHOLAR_LECTURE.toString());
        list.add(AcademicianEnum.CHANGJIANG_SCHOLAR_YOUTH.toString());
        return list;
    }

    /**
     * 2021年科学院院士入库
     *
     * @return
     */
    @PostMapping("/addAcs")
    public String addAcs() throws IOException {
        List<PersonInfoTopy> personList = new ArrayList<>();

        List<PersonInfo> list = casCrawlerService.addData();
        log.info("新增科学院院士人数：{}", list.size());
        List<PersonInfo> list1 = casCrawlerService.addData1();
        log.info("新增2021年科学院外籍院士人数：{}", list1.size());
        if (CollectionUtils.isNotEmpty(list1)) {
            list.addAll(list1);
            log.info("新增2021年科学院院士总人数：{}", list.size());
        }

        if (CollectionUtils.isNotEmpty(list)) {
            for (PersonInfo personInfo : list) {
                PersonInfoTopy topy = new PersonInfoTopy();
                BeanUtil.copyProperties(personInfo, topy);
                personList.add(topy);
            }
        }

        for (PersonInfoTopy personInfoTopy : personList) {
            PersonTitleToyp personTitleToyp = new PersonTitleToyp();
            BeanUtil.copyProperties(personInfoTopy, personTitleToyp);
            LambdaQueryWrapper<PersonInfoTopy> infoWrapper = new LambdaQueryWrapper<>();
            infoWrapper.eq(PersonInfoTopy::getName, personInfoTopy.getName());
            infoWrapper.eq(PersonInfoTopy::getStatus, 1);
            infoWrapper.eq(PersonInfoTopy::getDelFlag, 1);
            List<PersonInfoTopy> infoTopyList = iYouthService.list(infoWrapper);
            if (CollectionUtils.isNotEmpty(infoTopyList)) {
                Boolean stamp = false;
//                int num = 0;
                //如果infoTopyList不为空 说明数据库中有名字相同的人
                for (PersonInfoTopy infoTopy : infoTopyList) {
                    //比较工作单位
                    Long companyId = infoTopy.getCompanyId();
                    LambdaQueryWrapper<Company> companyWrapper = new LambdaQueryWrapper<>();
                    companyWrapper.eq(Company::getId, companyId);
                    Company company = iCompanyService.getOne(companyWrapper);
                    if (company != null) {
                        //如果company不为null
                        //如果单位一样   那就认为是同一个人
                        if (StringUtils.equals(company.getCompanyName(), personInfoTopy.getWorkCompany())) {
                            //如果相同   认为是同一个人
                            //在title表中查询是否有数据
                            LambdaQueryWrapper<PersonTitleToyp> titleWrapper = new LambdaQueryWrapper<>();
                            titleWrapper.eq(PersonTitleToyp::getPersonId, infoTopy.getId());
                            titleWrapper.eq(PersonTitleToyp::getElectionYear, personInfoTopy.getElectionYear());
                            titleWrapper.eq(PersonTitleToyp::getAcademicianTitle, personInfoTopy.getAcademicianTitle());
                            List<PersonTitleToyp> titleList = personTitleToypService.list(titleWrapper);
                            if (CollectionUtils.isNotEmpty(titleList)) {
                                //如果titleList不为空  说明在title表有数据    不需要存数据库
                                stamp = true;
                                break;
                            } else {
                                //title表中没有数据   只在title表中存入一份数据即可
                                Long id = infoTopy.getId();
                                personTitleToyp.setPersonId(id);
                                personTitleToypService.save(personTitleToyp);
                                stamp = true;
                                break;
                            }
                        } else {
                            //如果工作单位不同   则说明不是同一个人
                            //在info，title，company，表都存数据
                            stamp = false;
                        }
                    } else if (company == null) {
                        //如果单位不一样    专业一样  也认为是同一个人
                        stamp = false;
                    }
                }
                if (stamp == false) {
                    //  如果不同   说明不是一个人
                    if (StringUtils.isNotBlank(personInfoTopy.getWorkCompany())) {
                        iYouthService.queryCompany(personInfoTopy);
                    }
                    iYouthService.save(personInfoTopy);
                    LambdaQueryWrapper<PersonInfoTopy> queryWrapper1 = new LambdaQueryWrapper<>();
                    queryWrapper1.eq(PersonInfoTopy::getName, personInfoTopy.getName());
                    queryWrapper1.eq(PersonInfoTopy::getMajor, personInfoTopy.getMajor());
                    List<PersonInfoTopy> list2 = iYouthService.list(queryWrapper1);
                    Long id = list2.get(0).getId();
                    personTitleToyp.setPersonId(id);
                    personTitleToypService.save(personTitleToyp);
                }
            } else {
                if (StringUtils.isNotBlank(personInfoTopy.getWorkCompany())) {
                    iYouthService.queryCompany(personInfoTopy);
                }
                iYouthService.save(personInfoTopy);
                LambdaQueryWrapper<PersonInfoTopy> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(PersonInfoTopy::getName, personInfoTopy.getName());
                queryWrapper.eq(PersonInfoTopy::getMajor, personInfoTopy.getMajor());
                List<PersonInfoTopy> list2 = iYouthService.list(queryWrapper);
                Long id = list2.get(0).getId();
                personTitleToyp.setPersonId(id);
                personTitleToypService.save(personTitleToyp);
            }
        }


//        for (PersonInfoTopy personInfoTopy : list) {
//            PersonTitleToyp personTitleToyp = new PersonTitleToyp();
//            BeanUtil.copyProperties(personInfoTopy, personTitleToyp);
//            LambdaQueryWrapper<PersonInfoTopy> infoWrapper = new LambdaQueryWrapper<>();
//            infoWrapper.eq(PersonInfoTopy::getName, personInfoTopy.getName());
//            infoWrapper.eq(PersonInfoTopy::getStatus, 1);
//            infoWrapper.eq(PersonInfoTopy::getDelFlag, 1);
//            List<PersonInfoTopy> infoTopyList = iYouthService.list(infoWrapper);
//            if (CollectionUtils.isNotEmpty(infoTopyList)) {
////                Boolean stamp = false;
//                int num = 0;
//                //如果infoTopyList不为空 说明数据库中有名字相同的人
//                for (PersonInfoTopy infoTopy : infoTopyList) {
//                    //比较工作单位
//                    Long companyId = infoTopy.getCompanyId();
//                    LambdaQueryWrapper<Company> companyWrapper = new LambdaQueryWrapper<>();
//                    companyWrapper.eq(Company::getId, companyId);
//                    Company company = iCompanyService.getOne(companyWrapper);
//                    if (company != null) {
//                        //如果company不为null
//                        //如果单位一样   比较专业
//                        if (StringUtils.equals(company.getCompanyName(), personInfoTopy.getWorkCompany())) {
//                            //判断专业是否相同
//                            if (StringUtils.equals(infoTopy.getMajor(), personInfoTopy.getMajor())) {
//                                //如果相同   认为是同一个人
//                                //在title表中存一份数据
//                                Long id = infoTopy.getId();
//                                personTitleToyp.setPersonId(id);
//                                personTitleToypService.save(personTitleToyp);
//                                break;
//                            } else {
////                                //  如果不同   说明不是一个人
////                                if (personInfoTopy.getAcademicianTitle() == 0) {
////                                    iYouthService.queryCompany(personInfoTopy);
////                                }
////                                iYouthService.save(personInfoTopy);
////                                LambdaQueryWrapper<PersonInfoTopy> queryWrapper1 = new LambdaQueryWrapper<>();
////                                queryWrapper1.eq(PersonInfoTopy::getName, personInfoTopy.getName());
////                                queryWrapper1.eq(PersonInfoTopy::getMajor, personInfoTopy.getMajor());
////                                queryWrapper1.eq(PersonInfoTopy::getAcademicianTitle, personInfoTopy.getAcademicianTitle());
////                                List<PersonInfoTopy> list2 = iYouthService.list(queryWrapper1);
////                                Long id = list2.get(0).getId();
////                                personTitleToyp.setPersonId(id);
////                                personTitleToypService.save(personTitleToyp);
//                                num+=1;
//                            }
//                        }
//                    } else {
//                        //如果单位不一样    专业一样  也认为是同一个人
//                        if (infoTopy.getMajor().equals(personInfoTopy.getMajor())) {
//                            Long id = infoTopy.getId();
//                            personTitleToyp.setPersonId(id);
//                            personTitleToypService.save(personTitleToyp);
//                            break;
//                        } else {
//                            map.put(personInfoTopy.getName(), personInfoTopy);
//                            break;
//                        }
//                    }
//            }
//                if (num == infoTopyList.size()){
//                    //  如果不同   说明不是一个人
//                    if (personInfoTopy.getAcademicianTitle() == 0) {
//                        iYouthService.queryCompany(personInfoTopy);
//                    }
//                    iYouthService.save(personInfoTopy);
//                    LambdaQueryWrapper<PersonInfoTopy> queryWrapper1 = new LambdaQueryWrapper<>();
//                    queryWrapper1.eq(PersonInfoTopy::getName, personInfoTopy.getName());
//                    queryWrapper1.eq(PersonInfoTopy::getMajor, personInfoTopy.getMajor());
//                    List<PersonInfoTopy> list2 = iYouthService.list(queryWrapper1);
//                    Long id = list2.get(0).getId();
//                    personTitleToyp.setPersonId(id);
//                    personTitleToypService.save(personTitleToyp);
//                }
//        } else{
//            if (personInfoTopy.getAcademicianTitle() == 0) {
//                iYouthService.queryCompany(personInfoTopy);
//            }
//            iYouthService.save(personInfoTopy);
//            LambdaQueryWrapper<PersonInfoTopy> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(PersonInfoTopy::getName, personInfoTopy.getName());
//            queryWrapper.eq(PersonInfoTopy::getMajor, personInfoTopy.getMajor());
//            List<PersonInfoTopy> list2 = iYouthService.list(queryWrapper);
//            Long id = list2.get(0).getId();
//            personTitleToyp.setPersonId(id);
//            personTitleToypService.save(personTitleToyp);
//        }
//    }

        return respJson(CommonEnum.SUCCESS);
    }

    /**
     * 11月12日科研诚信人员入库
     *
     * @return
     */
//    @PostMapping("/addIntegrity")
//    public String addIntegrity(){
//        try {
//            List<PersonInfoTopy> infoTopyList = parseExcelService.parseExcel();
//            for (PersonInfoTopy personInfoTopy : infoTopyList) {
//                PersonTitleToyp personTitleToyp = new PersonTitleToyp();
//                BeanUtil.copyProperties(personInfoTopy,personTitleToyp);
//                //存储工作单位
//                iYouthService.queryCompany(personInfoTopy);
//                iYouthService.save(personInfoTopy);
//                System.out.println("..............");
//                personTitleToyp.setPersonId(personInfoTopy.getId());
//                if (personTitleToyp.getPersonId()!=null) {
//                    personTitleToypService.save(personTitleToyp);
//                }
//            }
//        } catch (IOException e) {
//            log.error("addIntergrity Exception:{}",e);
//        }
//        return respJson(CommonEnum.SUCCESS);
//    }
//    @PostMapping("/saveIntegrity")
//    public String saveIntegrity() {
//        try {
//            List<PersonInfoTopy> perosnList = integrityCaseCrawlerService.parsePersonInfoIntegrityCaseExcel();
//            List<IntegrityCase> paperList = integrityCaseCrawlerService.parseIntegrityCaseExcel();
//            //存放case和personinfo信息
//            List<IntegrityCase> caseList = new ArrayList<>();
//            for (IntegrityCase integrityCase : paperList) {
//                List<PersonInfoTopy> list = new ArrayList<>();
//                for (PersonInfoTopy personInfoTopy : perosnList) {
//                    if (personInfoTopy.getCaseId().equals(integrityCase.getId())) {
//                        list.add(personInfoTopy);
//                    }
//                }
//                integrityCase.setPersonInfotopyList(list);
//                caseList.add(integrityCase);
//            }
//            System.out.println(caseList.size());
//            for (int i = 25; i < caseList.size(); i++) {
//                PersonTitleToyp personTitleToyp = new PersonTitleToyp();
//                List<PersonInfoTopy> personInfotopyList = caseList.get(i).getPersonInfotopyList();
//                //1.查询integritycase表   根据title和通报日期查询是否与integrityCase一致
//                LambdaQueryWrapper<IntegrityCase> caseWrapper = new LambdaQueryWrapper<>();
//                caseWrapper.eq(IntegrityCase::getTitle, caseList.get(i).getTitle());
//                caseWrapper.eq(IntegrityCase::getNotifyDate, caseList.get(i).getNotifyDate());
//                IntegrityCase aCase = iIntegrityCaseService.getOne(caseWrapper);
//                //如果aCase等于空 说明integritycase表中数据与integrityCase对象数据不一致  在integritycase表中存入数据
//                if (aCase == null) {
//                    iIntegrityCaseService.save(caseList.get(i));
//                    //查询personinfotoyp表   根据姓名和工作单位查询
//                    List<PersonInfoTopy> personInfotopyList1 = caseList.get(i).getPersonInfotopyList();
//                    for (PersonInfoTopy personInfoTopy : personInfotopyList1) {
//                        //查询姓名
//                        LambdaQueryWrapper<PersonInfoTopy> infoWrapper = new LambdaQueryWrapper<>();
//                        infoWrapper.eq(PersonInfoTopy::getName, personInfoTopy.getName());
//                        List<PersonInfoTopy> nameList = iYouthService.list(infoWrapper);
//                        if (CollectionUtils.isNotEmpty(nameList)) {
//                            //如果nameList不为空   说明数据库有这个名字   查询工作单位
//                            LambdaQueryWrapper<Company> companyWrapper = new LambdaQueryWrapper<>();
//                            companyWrapper.eq(Company::getId, nameList.get(0).getCompanyId());
//                            Company company = iCompanyService.getOne(companyWrapper);
//                            if (company != null) {
//                                //比较单位是否相同
//                                if (StringUtils.equals(company.getCompanyName(), personInfoTopy.getWorkCompany())) {
//                                    //如果相同说明是同一个人  不用入库
//                                    continue;
//                                } else {
//                                    //如果不同   说明不是同一个人
//                                    iYouthService.queryCompany(personInfoTopy);
//                                    iYouthService.save(personInfoTopy);
//                                    personTitleToyp.setPersonId(personInfoTopy.getId());
//                                    Long id = caseList.get(i).getId();
//                                    personTitleToyp.setCaseId(id);
//                                    personInfoTopy.setAcademicianTitle(AcademicianEnum.RESEARCH_INTEGRITY.getCode());
//                                    personInfoTopy.setAcademicianTitleValue(AcademicianEnum.RESEARCH_INTEGRITY.getValue());
//                                    personTitleToypService.save(personTitleToyp);
//                                }
//                            }
//                        } else {
//                            //如果为空   说明不是同一个人
//                            iYouthService.queryCompany(personInfoTopy);
//                            iYouthService.save(personInfoTopy);
//                            personTitleToyp.setPersonId(personInfoTopy.getId());
//                            Long id = caseList.get(i).getId();
//                            personTitleToyp.setCaseId(id);
//                            personInfoTopy.setAcademicianTitle(AcademicianEnum.RESEARCH_INTEGRITY.getCode());
//                            personInfoTopy.setAcademicianTitleValue(AcademicianEnum.RESEARCH_INTEGRITY.getValue());
//                            personTitleToypService.save(personTitleToyp);
//                        }
//                    }
//                } else {
//                    //如果aCase不等于空   说明integritycase表中数据与integrityCase对象数据一致  不存数据库integritycase表
//                    //查询personinfotoyp表   根据姓名和工作单位查询
//                    List<PersonInfoTopy> personInfotopyList1 = caseList.get(i).getPersonInfotopyList();
//                    for (PersonInfoTopy personInfoTopy : personInfotopyList1) {
//                        //查询姓名
//                        LambdaQueryWrapper<PersonInfoTopy> infoWrapper = new LambdaQueryWrapper<>();
//                        infoWrapper.eq(PersonInfoTopy::getName, personInfoTopy.getName());
//                        List<PersonInfoTopy> nameList = iYouthService.list(infoWrapper);
//                        if (CollectionUtils.isNotEmpty(nameList)) {
//                            //如果nameList不为空   说明数据库有这个名字   查询工作单位
//                            LambdaQueryWrapper<Company> companyWrapper = new LambdaQueryWrapper<>();
//                            companyWrapper.eq(Company::getId, nameList.get(0).getCompanyId());
//                            Company company = iCompanyService.getOne(companyWrapper);
//                            if (company != null) {
//                                //比较单位是否相同
//                                if (StringUtils.equals(company.getCompanyName(), personInfoTopy.getWorkCompany())) {
//                                    //如果相同说明是同一个人  不用入库
//                                    continue;
//                                } else {
//                                    //如果不同   说明不是同一个人
//                                    iYouthService.queryCompany(personInfoTopy);
//                                    iYouthService.save(personInfoTopy);
//                                    LambdaQueryWrapper<PersonTitleToyp> queryWrapper = new LambdaQueryWrapper<>();
//                                    queryWrapper.eq(PersonTitleToyp::getPersonId, personInfoTopy.getId());
//                                    queryWrapper.eq(PersonTitleToyp::getCaseId, aCase.getId());
//                                    PersonTitleToyp one = personTitleToypService.getOne(queryWrapper);
//                                    if (one == null) {
//                                        personTitleToyp.setPersonId(personInfoTopy.getId());
//                                        Long id = caseList.get(i).getId();
//                                        personTitleToyp.setCaseId(id);
//                                        personTitleToypService.save(personTitleToyp);
//                                    }
//                                }
//                            } else {
//                                //如果不同   说明不是同一个人
//                                iYouthService.queryCompany(personInfoTopy);
//                                iYouthService.save(personInfoTopy);
//                                LambdaQueryWrapper<PersonTitleToyp> queryWrapper = new LambdaQueryWrapper<>();
//                                queryWrapper.eq(PersonTitleToyp::getPersonId, personInfoTopy.getId());
//                                queryWrapper.eq(PersonTitleToyp::getCaseId, aCase.getId());
//                                PersonTitleToyp one = personTitleToypService.getOne(queryWrapper);
//                                if (one == null) {
//                                    personTitleToyp.setPersonId(personInfoTopy.getId());
//                                    Long id = caseList.get(i).getId();
//                                    personTitleToyp.setCaseId(id);
//                                    personTitleToypService.save(personTitleToyp);
//                                }
//                            }
//                        } else {
//                            //如果为空   说明不是同一个人
//                            iYouthService.queryCompany(personInfoTopy);
//                            iYouthService.save(personInfoTopy);
//                            LambdaQueryWrapper<PersonTitleToyp> queryWrapper = new LambdaQueryWrapper<>();
//                            queryWrapper.eq(PersonTitleToyp::getPersonId, personInfoTopy.getId());
//                            queryWrapper.eq(PersonTitleToyp::getCaseId, aCase.getId());
//                            PersonTitleToyp one = personTitleToypService.getOne(queryWrapper);
//                            if (one == null) {
//                                personTitleToyp.setPersonId(personInfoTopy.getId());
//                                Long id = caseList.get(i).getId();
//                                personTitleToyp.setCaseId(id);
//                                personTitleToypService.save(personTitleToyp);
//                            }
//                        }
//                    }
//                }
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return respJson(CommonEnum.SUCCESS);
//    }

//    @PostMapping("/updatePersonInfo")
//    public String saveOrUpdate(){
//        try {
//            List<PersonInfoTopy> list = integrityCaseCrawlerService.parsePersonInfoIntegrityCaseExcel();
//            for (PersonInfoTopy personInfoTopy : list) {
//                LambdaQueryWrapper<PersonInfoTopy> queryWrapper = new LambdaQueryWrapper<>();
//                queryWrapper.eq(PersonInfoTopy::getName,personInfoTopy.getName());
//
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return respJson(CommonEnum.SUCCESS);
//    }

//    @PostMapping("/saveOrUpdateScholar")
//    public String saveOrUpdateScholar(){
//        try {
//            List<PersonInfoTopy> list = scholarService.parseScholarExcel();
//            for (int i = 0; i <list.size() ; i++) {
//                LambdaQueryWrapper<PersonInfoTopy> queryWrapper = new LambdaQueryWrapper<>();
//                queryWrapper.eq(PersonInfoTopy::getId,list.get(i).getId());
//                PersonInfoTopy personInfoTopy = iYouthService.getOne(queryWrapper);
//                if (personInfoTopy != null){
//                    iYouthService.queryCompany(list.get(i));
//                    iYouthService.saveOrUpdate(list.get(i));
//                    saveSocial(list.get(i));
//                    saveHonor(list.get(i));
//                }
//            }
//        } catch (IOException e) {
//            log.error("ScholarExcel  长江学者补充数据excel异常:{}",e);
//        }
//        return respJson(CommonEnum.SUCCESS);
//    }

    private void saveHonor(PersonInfoTopy personInfoTopy) {
        if (StringUtils.isNotBlank(personInfoTopy.getHonor())){
            LambdaQueryWrapper<Honor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Honor::getPersonId,personInfoTopy.getId());
            queryWrapper.eq(Honor::getPrizeName,personInfoTopy.getHonor());
            int count = iHonorService.count(queryWrapper);
            if (count <= 0){
                Honor honor = new Honor();
                String prizeName = personInfoTopy.getHonor();
                if (prizeName.length()>1024){
//                    for (int i = 0; i < prizeName.length() ; i+=1000) {
//                        Honor honor1 = new Honor();
//                        honor1.setPersonId(personInfoTopy.getId());
//                        honor1.setPrizeName(prizeName.substring(i,i+1000));
//                        iHonorService.save(honor1);
//                    }
                    honor.setPrizeName(prizeName.substring(0,1000));
                    Honor honor1 = new Honor();
                    honor1.setPersonId(personInfoTopy.getId());
                    honor1.setPrizeName(prizeName.substring(1000));
                    iHonorService.save(honor);
                    iHonorService.save(honor1);
                }else {
                    honor.setPersonId(personInfoTopy.getId());
                    honor.setPrizeName(personInfoTopy.getHonor());
                    iHonorService.save(honor);
                }
            }
        }
    }

    private void saveSocial(PersonInfoTopy personInfoTopy) {
        if (StringUtils.isNotBlank(personInfoTopy.getSocialService())) {
            LambdaQueryWrapper<Social> socialWrapper = new LambdaQueryWrapper<>();
            socialWrapper.eq(Social::getPersonId, personInfoTopy.getId());
            socialWrapper.eq(Social::getDuties, personInfoTopy.getSocialService());
            int count = iSocialService.count(socialWrapper);
            if (count <= 0) {
                Social social = new Social().builder().personId(personInfoTopy.getId()).duties(personInfoTopy.getSocialService()).build();
                iSocialService.save(social);
            }
        }
    }
}
